﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
	public partial interface IService
{
[OperationContract]
string CRM_Contact_Add(CRM_Contact model ,int ACID);


[OperationContract]
string CRM_Contact_Update(CRM_Contact model ,int ACID);


[OperationContract]
string CRM_ContactByContactID_Delete(int ContactID,int ACID);


[OperationContract]
CRM_Contact CRM_ContactByContactID_Get(int ContactID,int ACID );


[OperationContract]
List<CRM_Contact> CRM_Contact_Get(string sortBy,int ACID);




}
public partial class Service : IService
{
#region  添加CRM_Contact表数据
/// <summary>
/// 添加CRM_Contact
/// </summary>
/// <param name="model">CRM_Contact实体</param>
/// <returns>空或错误</returns>
public string CRM_Contact_Add(CRM_Contact model,int ACID)
{
if(model.ContactID==null )
{
return "『联系人ID』不能为空！";
}
if(model.ClientID==null || model.ClientID.Equals(Guid.Empty))
{
return "『客户ID』不能为空！";
}
if(model.IsMain==null )
{
return "『主联系人』不能为空！";
}
if(string.IsNullOrEmpty(model.Name))
{
return "『姓名』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.CRM_Contact.Count(p=>p.ContactID==model.ContactID)>0)
{
return "主键已存在！";
}
ctx.CRM_Contact.InsertOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 添加失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  添加CRM_Contact表数据事物用重构
/// <summary>
/// 添加CRM_Contact事物用重构
/// </summary>
/// <param name="model">CRM_Contact实体</param>
/// <returns>空或错误</returns>
private string CRM_Contact_Add(CRM_Contact model,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(model.ContactID==null )
{
return "『联系人ID』不能为空！";
}
if(model.ClientID==null || model.ClientID.Equals(Guid.Empty))
{
return "『客户ID』不能为空！";
}
if(model.IsMain==null )
{
return "『主联系人』不能为空！";
}
if(string.IsNullOrEmpty(model.Name))
{
return "『姓名』不能为空！";
}
if(ctx.CRM_Contact.Count(p=>p.ContactID==model.ContactID)>0)
{
return "主键已存在！";
}
ctx.CRM_Contact.InsertOnSubmit(model);
return string.Empty;
}
#endregion



#region  修改CRM_Contact表数据
/// <summary>
/// 修改CRM_Contact
/// </summary>
/// <param name="model">CRM_Contact实体</param>
/// <returns>空或错误</returns>
public string CRM_Contact_Update(CRM_Contact model,int ACID)
{
if(model.ContactID==null )
{
return "『联系人ID』不能为空！";
}
if(model.ClientID==null || model.ClientID.Equals(Guid.Empty))
{
return "『客户ID』不能为空！";
}
if(model.IsMain==null )
{
return "『主联系人』不能为空！";
}
if(string.IsNullOrEmpty(model.Name))
{
return "『姓名』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.CRM_Contact.Count(p=>p.ContactID==model.ContactID)!= 1)
{
return "未找到指定记录或记录不唯一！";
}
CRM_Contact CRM_Contactmodel=ctx.CRM_Contact.Single(p => p.ContactID==model.ContactID);
if(model.ContactID!=null )
{
CRM_Contactmodel.ContactID=model.ContactID;
}
if(model.ClientID!=null || model.ClientID.Equals(Guid.Empty))
{
CRM_Contactmodel.ClientID=model.ClientID;
}
if(!string.IsNullOrEmpty(model.Name))
{
CRM_Contactmodel.Name=model.Name;
}
if(!string.IsNullOrEmpty(model.ZJM))
{
CRM_Contactmodel.ZJM=model.ZJM;
}
if(!string.IsNullOrEmpty(model.Mobile))
{
CRM_Contactmodel.Mobile=model.Mobile;
}
if(!string.IsNullOrEmpty(model.TEL))
{
CRM_Contactmodel.TEL=model.TEL;
}
if(!string.IsNullOrEmpty(model.QQ))
{
CRM_Contactmodel.QQ=model.QQ;
}
if(!string.IsNullOrEmpty(model.Email))
{
CRM_Contactmodel.Email=model.Email;
}
if(!string.IsNullOrEmpty(model.Fax))
{
CRM_Contactmodel.Fax=model.Fax;
}
if(!string.IsNullOrEmpty(model.Msn))
{
CRM_Contactmodel.Msn=model.Msn;
}
if(!string.IsNullOrEmpty(model.Department))
{
CRM_Contactmodel.Department=model.Department;
}
if(!string.IsNullOrEmpty(model.Headship))
{
CRM_Contactmodel.Headship=model.Headship;
}
if(!string.IsNullOrEmpty(model.Website))
{
CRM_Contactmodel.Website=model.Website;
}
if(model.IsMain!=null )
{
CRM_Contactmodel.IsMain=model.IsMain;
}
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 修改失败!错误详细信息：" + ex.Message;
}
}
}
#endregion



#region  删除CRM_Contact表ByContactID数据
/// <summary>
/// 删除CRM_ContactByContactID
/// </summary>
/// <param name="ContactID">联系人ID</param>
/// <returns>空或错误</returns>
public string CRM_ContactByContactID_Delete(int ContactID,int ACID)
{
if(ContactID==null )
{
return "『联系人ID』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.CRM_Contact.Count(p=>p.ContactID==ContactID)!=1)
{
return null;
}
CRM_Contact model=ctx.CRM_Contact.Single(p => p.ContactID== ContactID);
ctx.CRM_Contact.DeleteOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 删除失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  删除CRM_Contact表ByContactID数据[事物用重构]
/// <summary>
/// 删除CRM_ContactByContactID[事物用重构]
/// </summary>
/// <param name="ContactID">联系人ID</param>
/// <returns>空或错误</returns>
private string CRM_ContactByContactID_Delete(int ContactID,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(ContactID==null )
{
return "『联系人ID』不能为空！";
}
if(ctx.CRM_Contact.Count(p=>p.ContactID==ContactID)!=1)
{
return null;
}
CRM_Contact model=ctx.CRM_Contact.Single(p => p.ContactID== ContactID);
ctx.CRM_Contact.DeleteOnSubmit(model);
return string.Empty;
}
#endregion



#region  获取CRM_Contact表ByContactID数据
/// <summary>
/// 获取CRM_ContactByContactID
/// </summary>
/// <param name="ContactID">联系人ID</param>
/// <returns>当前表</returns>
public CRM_Contact CRM_ContactByContactID_Get(int ContactID,int ACID)
{
if(ContactID==null )
{
return null;
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.CRM_Contact.Count(p=>p.ContactID==ContactID)!=1)
{
return null;
}
return ctx.CRM_Contact.SingleOrDefault(p=>p.ContactID==ContactID);
}
catch (Exception ex)
{
return null;
}
}
}
#endregion



#region  获取CRM_Contact表数据
/// <summary>
/// 获取CRM_Contact
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<CRM_Contact></returns>
public List<CRM_Contact> CRM_Contact_Get(Expression<Func<CRM_Contact, bool>> predicate , string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.CRM_Contact.ToList();
}
 else 
{
return ctx.CRM_Contact.OrderBy(sortBy).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.CRM_Contact.Where(predicate).ToList();
}
 else 
{
return ctx.CRM_Contact.Where(predicate).OrderBy(sortBy).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  获取CRM_Contact表数据
/// <summary>
/// 获取CRM_Contact
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<CRM_Contact></returns>
public List<CRM_Contact> CRM_Contact_Get(string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
return CRM_Contact_Get(null,sortBy, ACID);
}
catch (Exception)
{
return null;
}
}
}
#endregion


#region  获取CRM_Contact表数据计数
/// <summary>
/// 获取CRM_Contact表数据计数
/// </summary>
/// <returns>List<CRM_Contact></returns>
public int CRM_Contact_GetCount(Expression<Func<CRM_Contact, bool>> predicate,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
return ctx.CRM_Contact.Count();

}
 else
{
return ctx.CRM_Contact.Where(predicate).Count();
}
}
catch 
{
return 0;
}
}
}
#endregion




#region  获取CRM_Contact分页表数据
/// <summary>
/// 获取CRM_Contact分页表数据
/// </summary>
/// <param name="pageIndex">当前页</param>
/// <param name="pageSize">页大小</param>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<CRM_Contact></returns>
public List<CRM_Contact> CRM_Contact_Getpage(int pageIndex, int pageSize,Expression<Func<CRM_Contact, bool>> predicate ,string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.CRM_Contact.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.CRM_Contact.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.CRM_Contact.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.CRM_Contact.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  批量修改CRM_Contact表数据
/// <summary>
/// 批量修改CRM_Contact
/// </summary>
/// <param name="predicate">Expression<Func<CRM_Contact, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<CRM_Contact,CRM_Contact>> 更新表达式树</param>
/// <param name="returns">影响记录数</param>
public int CRM_Contact_batchUpdate(Expression<Func<CRM_Contact, bool>> predicate,Expression<Func<CRM_Contact, CRM_Contact>> updater,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.CRM_Contact.Update(predicate, updater);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量修改CRM_Contact表数据事物用重构
/// <summary>
/// 批量修改CRM_Contact事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<CRM_Contact, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<CRM_Contact,CRM_Contact>> 更新表达式树</param>
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int CRM_Contact_batchUpdate(Expression<Func<CRM_Contact, bool>> predicate,Expression<Func<CRM_Contact, CRM_Contact>> updater,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.CRM_Contact.Update(predicate, updater);
return ok;
}
#endregion


#region  批量删除CRM_Contact表数据
/// <summary>
/// 批量删除CRM_Contact
/// </summary>
/// <param name="predicate">Expression<Func<CRM_Contact, bool>></param> 查找表达式树
/// <param name="returns">影响记录数</param>
public int CRM_Contact_batchDel(Expression<Func<CRM_Contact, bool>> predicate,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.CRM_Contact.Delete(predicate);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量删除CRM_Contact表数据事物用重构
/// <summary>
/// 批量删除CRM_Contact事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<CRM_Contact, bool>></param> 查找表达式树
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int CRM_Contact_batchDel(Expression<Func<CRM_Contact, bool>> predicate,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.CRM_Contact.Delete(predicate);
return ok;
}
#endregion


}
}

